//
//  STBannerView.m
//  赛拓
//
//  Created by 王磊 on 7/28/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STBannerView.h"
#import "STBannerBean.h"
@interface STBannerView()<UIScrollViewDelegate>

@property (nonatomic ,strong) UIScrollView *scrollView;

@property (nonatomic ,strong) UIView *bottomView;

@property (nonatomic ,strong) UILabel *titleLabel;

@property (nonatomic ,strong) UIPageControl *pageControl;

@end
#define BottomView_Height 30
#define margin 10
@implementation STBannerView

- (void)commitInit {
    [super commitInit];
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    
    self.scrollView = scrollView;
    
    scrollView.showsVerticalScrollIndicator = false;
    
    scrollView.showsHorizontalScrollIndicator = false;
    
    scrollView.delegate = self;
    
    scrollView.bounces = false;
    
    scrollView.pagingEnabled = true;
    
    [self addSubview:scrollView];
    
    UIView *bottomView = [[UIView alloc]init];
    
    self.bottomView = bottomView;
    
    bottomView.backgroundColor = [UIColor blackColor];
    
    bottomView.alpha = 0.6;
    
    [self addSubview:bottomView];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    
    self.titleLabel = titleLabel;
    
    titleLabel.textColor = [UIColor whiteColor];
    
    [self addSubview:titleLabel];
    
    UIPageControl *pageControl = [[UIPageControl alloc]init];
    
    self.pageControl = pageControl;
    
    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    
    pageControl.pageIndicatorTintColor = ST_999999_COLOR;
    
    [self addSubview:pageControl];
}

- (void)updateData:(id)data {
    
    if (self.dataArray.count) {
        [self.dataArray removeAllObjects];
        
        [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj removeFromSuperview];
        }];
    }
    [self.dataArray addObjectsFromArray:data];
    
    self.scrollView.contentSize = CGSizeMake(ST_SCREEN_WIDTH * self.dataArray.count, self.k_height);
    
    for (int i = 0 ; i < self.dataArray.count; i++) {
        
        STBannerBean *banner = self.dataArray[i];
        
        UIImageView *imageView = [[UIImageView alloc]init];
        
        [imageView set_BGBannerImageViewWithString:banner.imgUrl];
        
        [self.scrollView addSubview:imageView];
        
        imageView.tag = i + 100;
        
        imageView.userInteractionEnabled = true;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBanner:)];
        
        [imageView addGestureRecognizer:tap];
        
    }
    self.pageControl.numberOfPages = self.dataArray.count;
    
    self.pageControl.currentPage = 0;
    
    STBannerBean *banner = self.dataArray[0];
    
    self.titleLabel.text = banner.title;
    
    @WeakObj(self)
    [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        obj.frame = CGRectMake(idx * ST_SCREEN_WIDTH, 0, ST_SCREEN_WIDTH, selfWeak.k_height);
    }];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.scrollView.frame = self.bounds;
    
    self.bottomView.frame = CGRectMake(0, self.k_height - BottomView_Height, ST_SCREEN_WIDTH, BottomView_Height);
    
    @WeakObj(self)
    [self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        obj.frame = CGRectMake(idx * ST_SCREEN_WIDTH, 0, ST_SCREEN_WIDTH, selfWeak.k_height);
    }];
    
    self.pageControl.frame = CGRectMake(0, self.k_height - BottomView_Height, ST_SCREEN_WIDTH, BottomView_Height);
    //
    self.titleLabel.frame = CGRectMake(margin, self.k_height - BottomView_Height, ST_SCREEN_WIDTH / 2 - 10, BottomView_Height);
    
}
- (void)tapBanner:(UITapGestureRecognizer *)gr {
    
    if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(onItemClick:withData:forIndexPath:)]) {
        
        [self.mDelegate onItemClick:STItemClickTypeBanner withData:self.dataArray[gr.view.tag - 100] forIndexPath:nil];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSInteger page = scrollView.contentOffset.x / ST_SCREEN_WIDTH;
    
    self.pageControl.currentPage = page;
    
    STBannerBean *banner = self.dataArray[page];
    
    self.titleLabel.text = banner.title;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(scrollViewWillBeginDragging:)]) {
        [self.mDelegate scrollViewWillBeginDragging:self.scrollView];
    }
}


@end
